﻿var provinceName;
var cityName;
var arrCity;
var areaName;
var arrArea;
var actionUrl = "../../sys/ashx/UserHandler.ashx?action=myuser";
var filterObj;
var dtype;
var dtypecn;
var ulevel;
var lv;
var uname;
function getArgs(name) {
    var args = {};
    var match = null;
    var search = decodeURIComponent(location.search.substring(1));
    var reg = /(?:([^&]+)=([^&]+))/g;
    while ((match = reg.exec(search)) !== null) {
        args[match[1]] = match[2];
    }
    return args;
    //return search;
}
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}

function changetypecn()
{
    if (dtype == "YBQ")
        return "靶标害虫";
    else if (dtype == "CBD")
        return "测报灯";
    else if (dtype == "XQH")
        return "小气候气象仪";

}
$(function () {
    //InitSelect();
    dtype = getUrlParam("devtype");
    dtypecn = changetypecn();
    var size = { width: $(window).width(), height: $(window).height() };
    mylayout.init(size);
    areatree.init();
    mygrid.databind(size, filterObj);
    //autoResize({ dataGrid: '#userGrid', gridType: 'datagrid', callback: mygrid.databind, height: 36, width: 230 });
    $(window).resize(function () {
        size = { width: $(window).width(), height: $(window).height() };
        mylayout.resize(size);
    });
       
});
var mylayout = {
    init: function (size) {
        $('#layout').width(size.width - 4).height(size.height - 4).layout();
        var center = $('#layout').layout('panel', 'center');
        center.panel({
            onResize: function (w, h) {
                $('#userGrid').datagrid('resize', { width: w, height: h});
            }
        });
    },
    resize: function (size) {
        mylayout.init(size);
        $('#layout').layout('resize');
    }
};
var areatree = {
    init: function () {
        $('#deptree').tree({
            lines: true,
            url: '/ashx/AreaHandler.ashx?isadd=1',
            animate: true,
            onLoadSuccess: function (node, data) {
                if (node == null)
                    $('body').data('areaData', data);
            },
            onBeforeExpand: function (node) {                
                $('#deptree').tree('options').url = "/ashx/AreaHandler.ashx?keyid=" + node.id;
            },
            onClick: function (node) {                
                if (node.attributes != null) {
                    lv = node.attributes.ulevel;
                    ulevel = node.id;
                    uname = $("#uname").val();
                    getopt();
                }
                else {
                    filterObj = "";
                }
                $('#userGrid').datagrid('load', { filter: JSON.stringify(filterObj), devtype: dtype });
            }
        });
    },
    data: function () {
        var d = JSON.stringify($('body').data('areaData'));        
        return JSON.parse(d);
    }

};
var mygrid = {
    databind: function (size,opt) {        
        $('#userGrid').datagrid({
            //url: actionUrl + "?filter=" + JSON.stringify(opt),
            url: actionUrl,
            width: size.width-220,
            height: size.height,
            idField: 'KeyId',
            singleSelect: true,
            striped: true,
            rownumbers:true,
            queryParams: {
                filter: JSON.stringify(filterObj),
                devtype: dtype
            },
            columns: [[
                //{ title: 'ID', field: 'KeyId', width: 80, sortable: true },
                { title: '用户名', field: 'UserName', width: 140, sortable: true },
                { title: '真实姓名', field: 'TrueName', width: 120, sortable: true },
                { title: '部门名称', field: 'depname', width: 160 },                
                {
                    title: '地址', field: 'madd', width: 200                    
                },                
                {
                    title: '用户权限等级', field: 'ulevel', width: 100, formatter: function (value, row, index) {
                        if (value == 0) {
                            return "普通用户";
                        } else if (value == 1) {
                            return "县级用户";
                        } else if (value == 2) {
                            return "市级用户";
                        } else if (value == 3) {
                            return "省级用户";
                        } else if (value == 4) {
                            return "全国用户";
                        } else if (value == 5) {
                            return "管理员";
                        }
                    }
                },
                {
                    title: '查看设备', field: 'v', width: 200, formatter: function (v, d, i) {
                        if(dtype=="YBQ")
                            return '<a href="#" onclick="top.addTab(\'' + dtypecn + d.UserName + '\',\'status_ybq/YBQ.aspx?uid=' + d.KeyId + '&navid=42\', \'icon icon-server\');">' + dtypecn + '</a>';
                        if (dtype == "CBD")
                            return '<a href="#" onclick="top.addTab(\'' + dtypecn + d.UserName + '\',\'../status_cbd/DataTestCBD.aspx?uid=' + d.KeyId + '&navid=45\', \'icon icon-server\');">' + dtypecn + '</a>';
                        if (dtype == "XQH")
                            return '<a href="#" onclick="top.addTab(\'' + dtypecn + d.UserName + '\',\'../status_xqh/XQH.aspx?uid=' + d.KeyId + '&navid=44\', \'icon icon-server\');">' + dtypecn + '</a>';

                    }
                },
            ]],
            pagination: true,
            pageSize: PAGESIZE,
            rowStyler: function (index, row, css) {
                if (row.UserName == "admin") {
                    return 'font-weight:bold;';
                }
            }
        });
    },
    reload: function () {
        $('#userGrid').datagrid('clearSelections').datagrid('reload');
    },
    selectRow: function () {
        return $('#userGrid').datagrid('getSelected');
    }
};
//初始化表格
function searchuser() {
    uname = $("#uname").val();
    getopt();
    $('#userGrid').datagrid('load', { filter:JSON.stringify(filterObj),devtype: dtype});
}

function getopt() {   
    var ruleArr = [];
    if (lv == 1) {
        sulevel = ulevel.toString().substr(0, 2);
        sulevel = "2," + sulevel;
        ruleArr.push({ "field": "ulevel", "op": "left", "data": sulevel });
    }
    else if (lv == 2) {
        sulevel = ulevel.toString().substr(0, 4);
        sulevel = "4," + sulevel;
        ruleArr.push({ "field": "ulevel", "op": "left", "data": sulevel });
    }
    else if (lv == 3) {
        sulevel = ulevel;
        ruleArr.push({ "field": "ulevel", "op": "eq", "data": sulevel });        
    }
    if (uname != "")
    {
        ruleArr.push({ "field": "username", "op": "cn", "data": uname});
    }
    
    if (ruleArr.length > 0)
        filterObj = { groupOp: 'AND', rules: ruleArr };
    else
        filterObj = "";
    
}